﻿using System;
using System.Collections;
using System.Data;
using System.Text;

namespace Coffee.Lists
{
    public class cls_Table
    {
        TSoft.Data.DataLayer DBLayer;
        public cls_Table()
        {
            DBLayer = new TSoft.Data.DataLayer();
        }

        public DataTable GetDataByPositionId(int v_Pos)
        {
            return DBLayer.ExecDataTable("sp_Lst_Table", "Command,PositionID", "GetDataByPositionId", v_Pos);
        }

        public void Update(ref System.Data.DataTable Data)
        {
            try
            {
                Hashtable output = null;
                DBLayer.BeginTransaction();

                foreach (DataRow row in Data.Rows)
                {
                    if (row.RowState == DataRowState.Added || row.RowState == DataRowState.Modified)
                    {
                        DBLayer.ExecNonQuery("sp_Lst_Table", "Command,Id,Name,PositionID,Notes", ref output, "Update",
                            TSoft.Cvert.ToInt32(row["Id"], 0), TSoft.Cvert.ToString(row["Name"], ""),
                            TSoft.Cvert.ToInt32(row["PositionID"], 0), TSoft.Cvert.ToString(row["Notes"], ""));

                        if (!object.Equals(output, null) && output.Count > 0)
                            row["Id"] = TSoft.Cvert.ToInt32(output["ID"], 0);
                    }
                }

                DBLayer.CommitTransaction();
            }
            catch
            {
                DBLayer.RollbackTransaction();
            }
        }

        public void DeleteRow(DataRow Row)
        {
            DBLayer.ExecNonQuery("sp_Lst_Table", "Command,ID", "DeleteRow", TSoft.Cvert.ToInt32(Row["ID"], 0));
        }
    }
}
